package com.ease.component.managers {

    import com.ease.component.constant.IconConstant;

    import flash.display.DisplayObject;

    import mx.controls.Alert;
    import mx.core.FlexGlobals;
    import mx.managers.PopUpManager;


    /**
     * Helper class to show a confirm action dialog by using Alert.show()
     * method.
     */
    public class ConfirmHelper {

        public static const PLACEHOLDER_COUNT : String = "%I_COUNT%";
        public static const PLACEHOLDER_NAME  : String = "%I_NAME%";

        private static const INFO_ICON : Class = IconConstant.BIG_INFO_ICON;
        private static const ERROR_ICON : Class = IconConstant.BIG_ERROR_ICON;
        private static const QUESTION_ICON : Class = IconConstant.BIG_QUESTION_ICON;
        private static const WARNING_ICON : Class = IconConstant.BIG_WARNING_ICON;

        /**
         * A convenience function to show an Alert.YES | Alert.NO confirmation alert.
         *
         * See Alert.show() for details on the closeHandler signature.
         *
         * Usage:
         * Basic -> show("title", message", callback);
         *      This will behave just like Alert.show().
         *
         * Item name/ count substitution -> show("title", "message", callback, items, "name");
         *      If there is 1 item, substitutes the first occurrence of '%I_NAME%' with the name
         *      of the first item.
         *      If there is more than 1 item and we're using the plural form (see pluralization
         *      below), substitutes the first occurence of '%I_COUNT%' with the item count.
         *
         * Pluralization - show("title", "message", callback, items, "name",
         *                      "plural title", "plural message");
         *      If items is provided and there are multiple items, we try to use
         *      the plural strings. Both have to be supplied or we fall back to singular.
         *
         * @param title
         * @param msg
         * @param closeHandler
         * @param itemNames the list of selected items we're about to act on.
         * @param titlePlural
         * @param msgPlural
         */
        private static function confirm(title:String, msg:String, closeHandler:Function,
                                       itemNames:Array = null,
                                       titlePlural:String = null, msgPlural:String = null,
                                       iconClass:Class = null, defaultButton:uint = 0x0000):void {
            // Defaults
            if (defaultButton == 0x0000) {
                defaultButton = Alert.NO;
            }

            var titleActual:String = title;
            var msgActual:String = msg;

            if (itemNames != null) {
                if (   itemNames.length > 1
                    && !(isStringNullOrEmpty(titlePlural) && isStringNullOrEmpty(msgPlural))) {
                    titleActual = titlePlural;
                    msgActual = msgPlural.replace(PLACEHOLDER_COUNT, itemNames.length);
                } else {
                    msgActual = msgActual.replace(PLACEHOLDER_NAME, itemNames[0]);
                }
            }

            Alert.show(msgActual, titleActual,
                       Alert.YES | Alert.NO, null,
                       closeHandler, iconClass, defaultButton);
        }

        /**
         * Displays a warning dialog with a message and a title
         */
        public static function showWarningDialog(title:String, message:String) : void {
           Alert.show(message, title, Alert.OK, null, null, WARNING_ICON);
        }

        /**
         * Displays a warning confirm dialog with a message and a title
         */
        public static function showWarningConfirmDialog(title:String, message:String, closeHandler:Function = null) : void {
           Alert.show(message, title, Alert.OK | Alert.CANCEL, null, closeHandler, WARNING_ICON);
        }

        /**
         * Displays a information dialog with a message and a title
         */
        public static function showInformationDialog( title:String, message:String, closeHandler:Function = null) : void {
           Alert.show( message, title,  Alert.OK, null, closeHandler, INFO_ICON );
        }
        
        public static function showErrorDialog(title:String, message:String, closeHandler:Function = null):void {
           Alert.show(message, title, Alert.OK, null, closeHandler, ERROR_ICON );
        }

        /**
         * Displays a error dialog with a message and a title
         */
        public static function showErrorConfirmDialog(title:String, message:String, closeHandler:Function):void {
           Alert.show(message, title, Alert.OK | Alert.CANCEL, null,
              closeHandler, ERROR_ICON, Alert.CANCEL);
        }

        /**
         * Displays a confirm dialog with a message and a title
         */
        public static function showConfirmDialog(title:String, message:String, closeHandler:Function):void {
            confirm(title, message, closeHandler, null, null, null, QUESTION_ICON );
        }

        private static function isStringNullOrEmpty( str : String ) : Boolean {
           if( str == null ) {
              return true;
           }
           else if( str.length == 0 ) {
              return true;
           }
           else {
              return false;
           }
        }
    }
}

